<template>
	<account-page :title="title" :desc="desc">
		<view class="account-form-box">
			<u-form :model="model" ref="uForm" :errorType="form.errorType">
				<u-form-item class="form-item" label="旧密码" prop="oldPassword" :label-position="form.labelPosition">
					<u-input v-model="model.oldPassword" placeholder="请输入旧密码" type="password" />
				</u-form-item>
				<u-form-item class="form-item" label="新密码" prop="newPassword" :label-position="form.labelPosition">
					<u-input v-model="model.newPassword" placeholder="请输入新密码" type="password" />
				</u-form-item>
				<u-form-item class="form-item" label="确认密码" prop="confirmPassword" :label-position="form.labelPosition">
					<u-input v-model="model.confirmPassword" placeholder="请确认新密码" type="password" />
				</u-form-item>
			</u-form>
			<u-gap height="40"></u-gap>
			<u-button type="primary" shape="circle" @tap="handleSubmit">确认修改</u-button>
		</view>
		<u-gap height="40"></u-gap>
		<u-toast ref="uToast" />
	</account-page>
</template>

<script>
	export default {
		name: 'EditPassword',
		data() {
			return {
				title: '修改密码',
				desc: '如忘记密码，请联系管理员重置',
				id: '',
				form: {
					errorType: ['message'],
					labelPosition: 'top'
				},
				model: {
					oldPassword: '',
					newPassword: '',
					confirmPassword: ''
				},
				rules: {
					oldPassword: [{
							required: true,
							message: '请输入旧密码',
							trigger: ['blur']
						}
						, {
							min: 6,
							max: 20,
							message: '密码长度在6到20个字符',
							trigger: [ 'blur']
						},
						{
							pattern: /^[0-9a-zA-Z]*$/g,
							transform(value) {
								return String(value);
							},
							message: '密码为字母或数字',
							trigger: [ 'blur']
						}
					],
					newPassword: [{
							required: true,
							message: '请设置新密码',
							trigger: ['blur']
						},
						{
							min: 8,
							max: 12,
							message: '密码长度在8-12个字符',
							trigger: ['blur']
						},
						{
							pattern: /^[0-9a-zA-Z]*$/g,
							transform(value) {
								return String(value);
							},
							message: '密码为字母或数字',
							trigger: ['blur']
						}
					],
					confirmPassword: [{
							required: true,
							message: '请再次输入新密码',
							trigger: ['blur']
						},
						{
							validator: (rule, value, callback) => {
								return value === this.model.newPassword;
							},
							message: '两次输入的密码不相等',
							trigger: ['blur']
						}
					]
				}
			};
		},
		onReady() {
			this.$refs.uForm.setRules(this.rules);
		},
		onLoad(option) {
			if (option.id) {
				this.id = option.id
			}
		},
		methods: {
			handleSubmit() {
				this.$refs.uForm.validate(valid => {
					if (valid) {
					} else {
						console.log('验证失败');
					}
				});
			}
		}
	}
</script>

<style lang="scss" scoped>
	page {
		height: 100%;
		background-color: #fff;
	}

	.account-form-box {
		height: 100%;
		background-color: #fff;
		padding: 0 32rpx;
	}

	button {
		margin-top: 60rpx;
	}
</style>
